<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Vaccine;
use App\Models\VaccineType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class VaccineController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $vaccineType = VaccineType::query()->get()->mapWithKeys(function ($item) {
            $typeEnum = \App\Models\VaccineType::$typeEnum;
            $crowdEnum = \App\Models\VaccineType::$crowdEnum;

            return [$item['id'] => $item['name']."(".$typeEnum[$item['type']]."/".$crowdEnum[$item['crowd']].")"];
        })->toArray();
        return Grid::make(new Vaccine(['type']), function (Grid $grid) use($vaccineType)  {
            $grid->column('id')->sortable();
            $grid->column('name');

            $grid->column('type.name','疫苗大类');
            $grid->column('type.crowd','成人/儿童')->using([1=>'儿童',2=>'成人']);
            $grid->column('type.type','免规/非免规')->using([1=>'免规',2=>'非免规']);

            $grid->column('intro','接种提醒');

            $grid->filter(function (Grid\Filter $filter) use($vaccineType) {
                $filter->equal('id');
                $filter->like('name','名称');
                $filter->equal('type_id','疫苗大类')->select($vaccineType);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Vaccine(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type_id');
            $show->field('intro');
            $show->field('age_max');
            $show->field('age_min');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $vaccineType = VaccineType::query()->get()->mapWithKeys(function ($item) {
            $typeEnum = \App\Models\VaccineType::$typeEnum;
            $crowdEnum = \App\Models\VaccineType::$crowdEnum;

            return [$item['id'] => $item['name']."(".$typeEnum[$item['type']]."/".$crowdEnum[$item['crowd']].")"];
        });
        return Form::make(new Vaccine(), function (Form $form) use($vaccineType) {
            $form->display('id');
            $form->text('name');
            $form->select('type_id')->options($vaccineType);
            $form->text('intro')->value('');
            $form->text('age_max');
            $form->text('age_min');
        });
    }
}
